//##############################################################################################################//
//##                                                                                                          ##//    
//## This control is implement for find the doctor                                                            ##//
//##                                                                                                          ##//
//## Author   : Rupinder Saini              Manish Prajapati    Amit Deshwal                                  ##//
//## Dated    : May 29,2006                 22-03-2006          July 27,2007                                  ##//
//##                                                            Changes :- Url Re-Writing Functionality Added ##//    
//##############################################################################################################//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Data.SqlClient;
public partial class Doctor_ratedDoctor : System.Web.UI.Page
{
    // creating an instance of manager class
    Manager manager = new Manager(true,"ConnectionString");
    const string noRecText = "No Records Found.";
    protected localFunction obj = new localFunction();
   

    #region Pageload

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Form.Attributes.Add("onSubmit", "javascript:showWorking()");


        Master.keywordsContent = "top doctors, doctors online, find a doctor, ask a doctor, doctor list, search doctors, board certified doctors, types of doctor, top doctors in United States";
        Master.descriptionContent = "Findadoc.com offers top doctors online, best doctors list for doctor finder. You can find a doctor or ask a doctor from our best board certified doctors from types of doctor, various categories of specialized doctor.";
        Master.titleContent = "Top Doctors, Find A Doctor, Search Doctors From Best Doctors List, Top Doctors In United States";


        cntPaging.PagingMode = PagingControl.Paging.pagingMode.QueryString;
        cntPaging.CombinedPage = true;
        Master.TdRight = false;

        //Make TopLoginIMG Visible to display horizontal login box
        //Added: 29th Jan
        Master.TopLoginIMG = false;

        string[] s = Request.QueryString.AllKeys;

        // to build href without CategoryName
        {
            lnkSevenDay.HRef = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_Last7Days.aspx";
            //lnkAllTime.HRef = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_AllTime.aspx";
            lnkAllTime.Visible = false;
        }

        if (Request.QueryString.HasKeys())
        {
            if (!string.IsNullOrEmpty(Request.QueryString["catname"])) // to built href when category name is available
            {
                hdnCategory.Value = Request.QueryString["catname"].ToString();
                lnkSevenDay.HRef = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_" + getUrl(hdnCategory.Value.Trim()) + "_Last7Days.aspx";

            }

            if (s.Length > 1 && s[1].Contains("DateCon") && Request.QueryString["DateCon"] == "true") // to check for last 7 days query with paging
            {
                if (string.IsNullOrEmpty(Request.QueryString["catname"]))  // Last7Days without CategoryName
                {
                    BindRateDoctor(true, "");

                }
                else //Last7Days With CategoryName
                {

                    BindRateDoctor(true, ReplaceUrl(hdnCategory.Value.Trim()));
                    lnkAllTime.HRef = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_" + getUrl(hdnCategory.Value.Trim()) + "_AllTime.aspx";
                    lnkAllTime.Visible = true;
                }

            }
            else
            {
                if (s.Length > 1 && s[1].Contains("DateCon") && Request.QueryString["DateCon"] == "false") // to check for last 7 days query with paging
                {
                    if (string.IsNullOrEmpty(Request.QueryString["catname"]))  // Last7Days without CategoryName
                    {
                        BindRateDoctor(true, "");
                    }
                    else //Last7Days With CategoryName
                    {
                        BindRateDoctor(false, ReplaceUrl(hdnCategory.Value.Trim()));
                    }

                }
                if (!string.IsNullOrEmpty(Request.QueryString["catname"]))
                {
                    BindRateDoctor(false, ReplaceUrl(hdnCategory.Value.Trim()));
                }
            }

            var sqlparam = new SqlParameter[] { new SqlParameter("@type", "DoctorCategory"), new SqlParameter("@pagename", hdnCategory.Value.Trim()), new SqlParameter("@pagenumber", "1") };
            var ds_seo = CustomRepository.CustumDataSetFunction("USP_GET_SEO_DATA", sqlparam);
            if (ds_seo.Tables[0].Rows.Count > 0)
            {
                Master.titleContent = ds_seo.Tables[0].Rows[0]["Title"].ToString();
                Master.keywordsContent = ds_seo.Tables[0].Rows[0]["KeyWords"].ToString();
                Master.descriptionContent = ds_seo.Tables[0].Rows[0]["Description"].ToString();
            }
        }
        else
        {

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            ds = manager.ExecuteQuery("SELECT ID,Name FROM tbl_speciality order by name", "tbl");
            dt = ds.Tables[0];
            System.String vLink = string.Empty;
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    vLink = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_" + getUrl(dr["Name"]).ToString().Trim() + ".aspx";
                    specialtylist.Items.Add(new ListItem(dr["name"].ToString(), vLink));
                }
            }

            var sqlparam = new SqlParameter[] { new SqlParameter("@type", "Page"), new SqlParameter("@pagename", "TopDoctors"), new SqlParameter("@pagenumber", "1") };
            var ds_seo = CustomRepository.CustumDataSetFunction("USP_GET_SEO_DATA", sqlparam);
            if (ds_seo.Tables[0].Rows.Count > 0)
            {
                Master.titleContent = ds_seo.Tables[0].Rows[0]["Title"].ToString();
                Master.keywordsContent = ds_seo.Tables[0].Rows[0]["KeyWords"].ToString();
                Master.descriptionContent = ds_seo.Tables[0].Rows[0]["Description"].ToString();
            }


        }
    }
    #endregion


    /// <summary>
    /// If DateCon is True so show the Last Seven days rating records in the grid
    /// </summary>
    protected void BindRateDoctor(bool DateCon,System.String catname)
    {
        //Bind Grid View 
        SqlParameter[] sqlParam = new SqlParameter[3];
        sqlParam[0] = new SqlParameter("@DateCon", SqlDbType.Bit);
        sqlParam[1] = new SqlParameter("@SpecialityID", SqlDbType.Int);
        sqlParam[2] = new SqlParameter("@speciName", SqlDbType.VarChar,200);
        
        //Pass parameter in store procedure (GetRate)
        if (DateCon)
        {
            //this condition execute for when we click on 7 days link button
            sqlParam[0].Value = 1;
        }
        else
        {
            //this condition execute for when we click on All Time link button
            sqlParam[0].Value = 0;
        }
        
        sqlParam[2].Value = catname;
        //pass parameter to paging control
        cntPaging.SqlParameterList = sqlParam;
        
            grdRateDoctor.Visible = true;
            lblerr.Visible = false;
        
            trspec.Visible = false;
            trresult.Visible = true;
            trerror.Visible = false;
            
    }
       

    #region Rowbound
    // bind the rating control
    protected void grdRateDoctor_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            #region commented

            //4-OCT-2006: REMOVED SHOWRATE USER CONTROL. NUMERICAL SCORE TO BE DISPLAYED
            //Control C = LoadControl("../includes/showRate.ascx");
            //includes_showRate rateCC = (includes_showRate)C;
            //if (grdRateDoctor.DataKeys[e.Row.RowIndex].Value.ToString().Trim() != "")
            //    rateCC.rate =Convert.ToDecimal(grdRateDoctor.DataKeys[e.Row.RowIndex].Value);
            //rateCC.totalScore = 100.00M;
            //e.Row.Cells[3].Controls.Add(rateCC);            

            ///K: 12 oct 2006
            ///if the doctors are rated below minimum rating they should be labeled as 'Not Rated'

            //e.Row.Cells[3].Text = String.Format("{0:F}",grdRateDoctor.DataKeys[e.Row.RowIndex].Value); 
            #endregion

            float fRateLimit = Convert.ToSingle(ConfigurationManager.AppSettings["MinRatingLimit"].ToString());
            float fOverallRate = Convert.ToSingle(grdRateDoctor.DataKeys[e.Row.RowIndex].Value);

            e.Row.Cells[3].Text = ((fOverallRate >= fRateLimit) ? (fOverallRate > 100) ? "100" : Math.Round(fOverallRate, 2).ToString() : "Not Rated");
            e.Row.Cells[3].ForeColor = ((fOverallRate >= fRateLimit) ? System.Drawing.Color.Blue : System.Drawing.Color.Red);
            ///k: code ends                    

        }
    } 
    #endregion

    #region pager

    //protected void grdRateDoctor_RowCreated(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.Pager)
    //    {
    //        Label lbl = new Label();
    //        //compose the label text
    //        TableCell TC = new TableCell();
    //        lbl.Text = "Displaying " + ((((grdRateDoctor.PageIndex + 1) * grdRateDoctor.PageSize) - (grdRateDoctor.PageSize)) + 1) + " to " + ((grdRateDoctor.PageIndex + 1) * grdRateDoctor.PageSize) + " of " + gridcount;

    //        //notice that the pager template renders one cell in which
    //        TC.Controls.Add(lbl);
    //        TC.Attributes.Add("align", "left");
    //        e.Row.Cells[0].Controls[0].Controls[0].Controls.AddAt(0, TC);
    //        ((Table)e.Row.Cells[0].Controls[0]).Attributes.Add("width", "100%");
    //    }
    //}

    #endregion

    protected void lnkAllspec_Click(object sender, EventArgs e)
    {

        specialtylist.Items.Clear();//Added at 28/03/2008 to fix duplicate specility
        #region New Code added By Prashant Tomar Augest 09,2007
        DataSet dsallSp = new DataSet();
        DataTable dtallSp = new DataTable();

        dsallSp = manager.ExecuteQuery(@"select name from tbl_Speciality union
                                    select name from tbl_PrimarySpeciality union
                                    select name from tbl_SecondarySpeciality", "tbl");
        dtallSp = dsallSp.Tables[0];
        System.String vLink = string.Empty;
        if (dtallSp.Rows.Count > 0)
        {
            foreach (DataRow drallSp in dtallSp.Rows)
            {
                vLink = Session["strHttp"].ToString().Trim() + "Doctor/top_doctors_speciality_" + getUrl(drallSp["Name"]).ToString().Trim() + ".aspx";
                specialtylist.Items.Add(new ListItem(drallSp["name"].ToString(), vLink));
            }
        }
        dsallSp = null;
        #endregion 

        #region Code Commented By Prashant Tomar 09/08/2007
        /* specialtylist.DataSource = manager.ExecuteQuery(@"select name from tbl_Speciality union
                                    select name from tbl_PrimarySpeciality union
                                    select name from tbl_SecondarySpeciality 
                                            ", "tbl");
            specialtylist.DataTextField = "Name";
            specialtylist.DataValueField = "name";
            specialtylist.DataBind(); */
          #endregion

            lnkAllspec.Visible = false;
            tdall.Visible = false;
    }
    protected void grdRateDoctor_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected override void Render(HtmlTextWriter writer)
    {
        litPaging.Text = cntPaging.RenderedHtml;
        base.Render(writer);
    }


    protected string getUrl(object st)
    {
        return st.ToString().Replace(" & ", " and ").Replace(" ", "_").ToLower().Replace(",", "-or-");
    }
    protected string ReplaceUrl(object st)
    {
        return st.ToString().Replace("_and_","_&_").Replace("_"," ").Replace("-or-", ",");
    }

}
